package com.c39.otc.open.api;

import com.cqfan.cloud.framework.jwt.component.JwtComponent;
import com.cqfan.cloud.framework.redis.client.RedisFacade;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

/**
 * @author JiaLing.Fan
 * @version 1.0
 * @date 2023/3/14 23:52
 * @description RemoteSessionTemplate
 */


@Component
@Slf4j
public class AppSessionTemplate {

    private ThreadLocal<String> threadLocal = new ThreadLocal<String>();

    @Resource
    private JwtComponent jwtComponent;

    @Resource
    private ObjectMapper objectMapper;

    @Resource
    private RedisFacade redisFacade;

    private String prefix = "app_session_";


    public String get() {
        String value = this.threadLocal.get();
        return get(value);
    }

    public String get(String sessionId) {
        //   String signature = JWT.decode(sessionId).getSignature();
        String key =  jwtComponent.getAudience(sessionId);
        jwtComponent.verifyToken(sessionId, key);
        this.threadLocal.set(sessionId);
        if(redisFacade.get(prefix + key) == null){
            return  null;
        }else{
            try {
                return  objectMapper.readValue(redisFacade.get(prefix + key), String.class);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }
    }


    public String getSign(String sessionId) {
        return get(sessionId);
    }

    public void put(String sessionId, String data) {
        String key =  jwtComponent.getAudience(sessionId);
        try {
            String dataStr =   objectMapper.writeValueAsString(data);
            redisFacade.set(prefix + key, dataStr);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public void putSign(String sessionId, String data) {
        try {
            String key =  jwtComponent.getAudience(sessionId);
            String dataStr = objectMapper.writeValueAsString(data);
            redisFacade.set(prefix + key, dataStr);
        } catch (IOException e) {
            throw new RuntimeException("未知异常");
        }
    }

    public void putSign(String sessionId, String data, int expires) {
        String key =  jwtComponent.getAudience(sessionId);
        String dataStr = null;
        try {
            dataStr = objectMapper.writeValueAsString(data);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        redisFacade.set(prefix + key, dataStr , expires);
    }

    public void remove(String sessionId) {
        String key =  jwtComponent.getAudience(sessionId);
        redisFacade.del(prefix + key);
        //this.properties.remove(sessionId);
    }

    public Map<String, String> gets() {
        //TODO 暂不支持从远程加载所有数据
        return null;
    }

    public void removes() {
        //TODO 暂不支持清空所有数据
        //  this.properties.clear();
    }

    public void currentThreadremove() {
        this.threadLocal.remove();
    }

}
